//
//  ASResearchVC.m
//  AttendenceSys
//
//  Created by 窦芳俊 on 16/4/13.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASResearchVC.h"
#import "ASAddResearchPaperVC.h"
#import "ASAddResearchMonographVC.h"
#import "ASAddResearchProjectVC.h"
#import "ASAddResearchPatentVC.h"
#import "ASAddResearchOtherVC.h"
#import "ASRPaperDetailVC.h"
#import "ASRMonographDetailVC.h"
#import "ASRProjectDetailVC.h"
#import "ASRPatentDetailVC.h"
#import "ASROtherDetailVC.h"


@interface ASResearchVC ()

@end

@implementation ASResearchVC

- (instancetype)initWithAchiTypeId:(NSInteger)typeId
                          typeName:(NSString *)typeName
                      researchType:(ASResearchTypeInfo *)rType
{
    self = [super init];
    if (self)
    {
        _achiTypeId = typeId;
        _achiTypeName = typeName;
        _rSelectedTypeInfo = rType;
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _researchType = _rSelectedTypeInfo.researchSubjectName;
    self.strNavTitle = _researchType;
    [self setRightText:@"增加"];
    
    [self initUI];
    
    [self fetchResearchList];
}

- (void)initUI
{
    UIImageView* bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.image = [UIImage imageNamed:@"bg_personal"];
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
    
    // infoTableView
    _researchTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _researchTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _researchTableView.delegate = self;
    _researchTableView.dataSource = self;
    _researchTableView.backgroundColor = [UIColor whiteColor];
    _researchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _researchTableView.rowHeight = kResearchCellHeight;
    [_researchTableView registerClass:[ASResearchCell class] forCellReuseIdentifier:NSStringFromClass([ASResearchCell class])];
    [bgView addSubview:_researchTableView];
}

- (void)fetchResearchList
{
    //获取用户成果相关数据
    //http://211.65.216.54/getUserCondition.action?userCode=01120603&conditonTypeId=1 -1表示全部，1专业实习，2志愿服务，3社会实践，4其他
    
    NSString* refer = @"getUserCondition.action?";
    NSString* prefix = kASHttpUrl(refer);
    NSString* userCode = [ASAppDelegate sharedAppDelegate].userInfo.userCode;
    NSString* achiTypeId = [NSString stringWithFormat:@"%ld", (long)_achiTypeId];
    NSString* totalUrl = [NSString stringWithFormat:@"%@userCode=%@&conditonTypeId=%@&researchSubjectId=%@",
                          prefix,
                          userCode,
                          achiTypeId,
                          _rSelectedTypeInfo.researchSubjectId];
    
    [SVProgressHUD showWithStatus:@"获取中..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    [networkingEngine GET:totalUrl parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            NSArray* data = [jsonRsp objectForKey:kASJsonData];
            
            [SVProgressHUD dismiss];
            
            [self parseData:data];
            [_researchTableView reloadData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

- (void)parseData:(NSArray*)arr
{
    if (_researchArray == nil)
    {
        _researchArray = [[NSMutableArray alloc] init];
    }
    
    // 此处要根据不同的科研类型 做不同的对应字段的解析 //@"论文", @"专著", @"项目", @"专利", @"其它"
    if ([_rSelectedTypeInfo.researchSubjectName isEqualToString:@"论文"])
    {
        [self parsePaper:arr];
    }
    else if ([_rSelectedTypeInfo.researchSubjectName isEqualToString:@"专著"])
    {
        [self parseMonograph:arr];
    }
    else if ([_rSelectedTypeInfo.researchSubjectName isEqualToString:@"项目"])
    {
        [self parseProject:arr];
    }
    else if ([_rSelectedTypeInfo.researchSubjectName isEqualToString:@"专利"])
    {
        [self parsePatent:arr];
    }
    else if ([_rSelectedTypeInfo.researchSubjectName isEqualToString:@"其它"])
    {
        [self parseOther:arr];
    }
}

- (void)parsePaper:(NSArray*)arr
{
    for (NSInteger i = 0; i < [arr count]; i++)
    {
        ASResearchInfo* info = [[ASResearchInfo alloc] init];
        
        info.researchA = [[arr objectAtIndex:i] objectForKey:@"attr1"];//论文名称
        info.researchB = [[arr objectAtIndex:i] objectForKey:@"attr2"];//期刊名称
        info.researchC = [[arr objectAtIndex:i] objectForKey:@"attr3"];//发表时间
        info.researchD = [[arr objectAtIndex:i] objectForKey:@"attr4"];//论文类别
        info.researchE = [[arr objectAtIndex:i] objectForKey:@"attr5"];//作者排名
        info.researchF = [[arr objectAtIndex:i] objectForKey:@"attr6"];//论文描述
        
//        info.researchA = [info.researchA stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//        info.researchB = [info.researchB stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//        info.researchC = [info.researchC stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//        info.researchD = [info.researchD stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//        info.researchE = [info.researchE stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//        info.researchF = [info.researchF stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        kASIsNil(info.researchA);
        kASIsNil(info.researchB);
        kASIsNil(info.researchC);
        kASIsNil(info.researchD);
        kASIsNil(info.researchE);
        kASIsNil(info.researchF);
        
        [_researchArray addObject:info];
    }
}

- (void)parseMonograph:(NSArray*)arr
{
    for (NSInteger i = 0; i < [arr count]; i++)
    {
        ASResearchInfo* info = [[ASResearchInfo alloc] init];
        
        info.researchA = [[arr objectAtIndex:i] objectForKey:@"attr1"];//著作名称
        info.researchB = [[arr objectAtIndex:i] objectForKey:@"attr2"];//出版号
        info.researchC = [[arr objectAtIndex:i] objectForKey:@"attr3"];//出版社名称
        info.researchD = [[arr objectAtIndex:i] objectForKey:@"attr4"];//出版社级别
        info.researchE = [[arr objectAtIndex:i] objectForKey:@"attr5"];//作者排名
        info.researchF = [[arr objectAtIndex:i] objectForKey:@"attr6"];//著作描述
        
        kASIsNil(info.researchA);
        kASIsNil(info.researchB);
        kASIsNil(info.researchC);
        kASIsNil(info.researchD);
        kASIsNil(info.researchE);
        kASIsNil(info.researchF);
        
        [_researchArray addObject:info];
    }
}

- (void)parseProject:(NSArray*)arr
{
    for (NSInteger i = 0; i < [arr count]; i++)
    {
        ASResearchInfo* info = [[ASResearchInfo alloc] init];
        
        info.researchA = [[arr objectAtIndex:i] objectForKey:@"attr1"];//项目名称
        info.researchB = [[arr objectAtIndex:i] objectForKey:@"attr2"];//立项单位
        info.researchC = [[arr objectAtIndex:i] objectForKey:@"attr3"];//立项时间
        info.researchD = [[arr objectAtIndex:i] objectForKey:@"attr4"];//结项时间
        info.researchE = [[arr objectAtIndex:i] objectForKey:@"attr5"];//项目级别
        info.researchF = [[arr objectAtIndex:i] objectForKey:@"attr6"];//成员排名
        info.researchG = [[arr objectAtIndex:i] objectForKey:@"attr7"];//项目描述
        
        kASIsNil(info.researchA);
        kASIsNil(info.researchB);
        kASIsNil(info.researchC);
        kASIsNil(info.researchD);
        kASIsNil(info.researchE);
        kASIsNil(info.researchF);
        kASIsNil(info.researchG);
        
        [_researchArray addObject:info];
    }
}

- (void)parsePatent:(NSArray*)arr
{
    for (NSInteger i = 0; i < [arr count]; i++)
    {
        ASResearchInfo* info = [[ASResearchInfo alloc] init];
        
        info.researchA = [[arr objectAtIndex:i] objectForKey:@"attr1"];//授权编号
        info.researchB = [[arr objectAtIndex:i] objectForKey:@"attr2"];//专利名称
        info.researchC = [[arr objectAtIndex:i] objectForKey:@"attr3"];//专利级别
        info.researchD = [[arr objectAtIndex:i] objectForKey:@"attr4"];//发明人排名
        info.researchE = [[arr objectAtIndex:i] objectForKey:@"attr5"];//专利描述
        
        kASIsNil(info.researchA);
        kASIsNil(info.researchB);
        kASIsNil(info.researchC);
        kASIsNil(info.researchD);
        kASIsNil(info.researchE);
        
        [_researchArray addObject:info];
    }
}

- (void)parseOther:(NSArray*)arr
{
    for (NSInteger i = 0; i < [arr count]; i++)
    {
        ASResearchInfo* info = [[ASResearchInfo alloc] init];
        
        info.researchA = [[arr objectAtIndex:i] objectForKey:@"attr1"];//成果名称
        info.researchB = [[arr objectAtIndex:i] objectForKey:@"attr2"];//成果时间
        info.researchC = [[arr objectAtIndex:i] objectForKey:@"attr3"];//成果描述
        
        kASIsNil(info.researchA);
        kASIsNil(info.researchB);
        kASIsNil(info.researchC);
        
        [_researchArray addObject:info];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

// 增加
- (void)rightButtonPressed:(id)action
{
//    ASAddResearchVC *addVC = [[ASAddResearchVC alloc] initWithTypeId:_achiTypeId researchType:_researchType];
//    addVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:addVC animated:YES];
    
    if ([_researchType isEqualToString:@"论文"])
    {
        ASAddResearchPaperVC *addVC = [[ASAddResearchPaperVC alloc] initWithTypeId:_achiTypeId
                                                                      researchType:_researchType
                                                                         rTypeInfo:_rSelectedTypeInfo];
        addVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:addVC animated:YES];
    }
    else if ([_researchType isEqualToString:@"专著"])
    {
        ASAddResearchMonographVC *addVC = [[ASAddResearchMonographVC alloc] initWithTypeId:_achiTypeId
                                                                              researchType:_researchType
                                                                                 rTypeInfo:_rSelectedTypeInfo];
        addVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:addVC animated:YES];
    }
    else if ([_researchType isEqualToString:@"项目"])
    {
        ASAddResearchProjectVC *addVC = [[ASAddResearchProjectVC alloc] initWithTypeId:_achiTypeId
                                                                          researchType:_researchType
                                                                             rTypeInfo:_rSelectedTypeInfo];
        addVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:addVC animated:YES];
    }
    else if ([_researchType isEqualToString:@"专利"])
    {
        ASAddResearchPatentVC *addVC = [[ASAddResearchPatentVC alloc] initWithTypeId:_achiTypeId
                                                                        researchType:_researchType
                                                                           rTypeInfo:_rSelectedTypeInfo];
        addVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:addVC animated:YES];
    }
    else if ([_researchType isEqualToString:@"其它"])
    {
        ASAddResearchOtherVC *addVC = [[ASAddResearchOtherVC alloc] initWithTypeId:_achiTypeId
                                                                      researchType:_researchType
                                                                         rTypeInfo:_rSelectedTypeInfo];
        addVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:addVC animated:YES];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_researchArray count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ASResearchCell class])];
    
    ASResearchCell *rCell = (ASResearchCell *)cell;
    [rCell updateResearchCellStatus:[_researchArray objectAtIndex:[indexPath row]] rTypeInfo:_rSelectedTypeInfo];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 根据不同的科研类型展示不同的详情视图
    if ([_researchType isEqualToString:@"论文"])
    {
        ASRPaperDetailVC *dVC = [[ASRPaperDetailVC alloc] initWithResearchInfo:[_researchArray objectAtIndex:[indexPath row]]
                                                                      typeName:_researchType];
        dVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:dVC animated:YES];
    }
    else if ([_researchType isEqualToString:@"专著"])
    {
        ASRMonographDetailVC *dVC = [[ASRMonographDetailVC alloc] initWithResearchInfo:[_researchArray objectAtIndex:[indexPath row]]
                                                                              typeName:_researchType];
        dVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:dVC animated:YES];
    }
    else if ([_researchType isEqualToString:@"项目"])
    {
        ASRProjectDetailVC *dVC = [[ASRProjectDetailVC alloc] initWithResearchInfo:[_researchArray objectAtIndex:[indexPath row]]
                                                                          typeName:_researchType];
        dVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:dVC animated:YES];
    }
    else if ([_researchType isEqualToString:@"专利"])
    {
        ASRPatentDetailVC *dVC = [[ASRPatentDetailVC alloc] initWithResearchInfo:[_researchArray objectAtIndex:[indexPath row]]
                                                                        typeName:_researchType];
        dVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:dVC animated:YES];
    }
    else if ([_researchType isEqualToString:@"其它"])
    {
        ASROtherDetailVC *dVC = [[ASROtherDetailVC alloc] initWithResearchInfo:[_researchArray objectAtIndex:[indexPath row]]
                                                                      typeName:_researchType];
        dVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:dVC animated:YES];
    }
}


@end




